package com.haidechizi.fastadmin.modules.sys.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.haidechizi.fastadmin.common.utils.Constant;
import com.haidechizi.fastadmin.common.utils.MapUtils;
import com.haidechizi.fastadmin.modules.sys.dao.SysMenuDao;
import com.haidechizi.fastadmin.modules.sys.entity.SysMenu;
import com.haidechizi.fastadmin.modules.sys.service.SysMenuService;
import com.haidechizi.fastadmin.modules.sys.service.SysRoleMenuService;
import com.haidechizi.fastadmin.modules.sys.service.SysUserService;
import com.haidechizi.fastadmin.modules.sys.shiro.ShiroUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenu> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenu> menuList = queryListParentId(parentId);
        if (CollectionUtils.isEmpty(menuIdList)) {
            return menuList;
        }
        List<SysMenu> userMenuList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<SysMenu> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    @Override
    public List<SysMenu> queryNotButtonList() {
        return baseMapper.queryNotButtonList();
    }

    @Override
    public List<SysMenu> getUserMenuList(Long userId) {
        //系统管理员，拥有最高权限
        if (userId == Constant.SUPER_ADMIN) {
            return getAllMenuList(null);
        }

        //用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public void delete(Long menuId) {
        //删除菜单
        this.deleteById(menuId);
        //删除菜单与角色关联
        sysRoleMenuService.deleteByMap(new MapUtils().put("menu_id", menuId));
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenu> getAllMenuList(List<Long> menuIdList) {
        //查询根菜单列表
        List<SysMenu> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<Long> menuIdList) {
        List<SysMenu> subMenuList = new ArrayList<SysMenu>();

        for (SysMenu entity : menuList) {
            //目录
            if (entity.getName().equals("菜单管理")
                    && ShiroUtils.getUserId() != Constant.SUPER_ADMIN
                    && (entity.getMenuId() != 1 || entity.getMenuId() != 2
                    || entity.getMenuId() != 3 || entity.getMenuId() != 4)
            )
                continue;
            if (entity.getType() == Constant.MenuType.CATALOG.getValue()) {
                entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }

    /**
     * 根据名称查询菜单
     *
     * @param name
     * @return
     */
    @Override
    public List<SysMenu> queryMenuByName(String name) {
        return baseMapper.selectList(new EntityWrapper<SysMenu>().eq("name", name));
    }

    /**
     * 删除子菜单
     *
     * @param menuId
     */
    @Override
    public void deleteChild(Long menuId) {
        //判断是否有子菜单或按钮
        List<SysMenu> menuList = this.queryListParentId(menuId);
        if (CollectionUtils.isNotEmpty(menuList)) {
            for (SysMenu menu : menuList) {
                this.deleteChild(menu.getMenuId());
            }
        }
        this.delete(menuId);
    }
}
